--#include "data\config\vip\VipConfig.lua" once
--#include "data\functions\welfare\OfflineExpCopy.lua"  once  		--离线副本
--#include "data\functions\Activity\Operation\operation.lua" once   --运营活动
--#include "data\functions\Vip\VipSceneCommon.lua" once   --vip挂机
--#include "data\functions\Activity\OpenServer\OpenServerPKRank.lua" once
--#include "data\functions\Activity\DailyActivity\NationwideRedPackets.lua" once    --全民红包

VIP_FUBEN_VIPLEVEL  =  1 			--VIP副本的VIP等级

function GmDoVip( sysarg, flag, param )
	print("GmDoVip, flag="..flag..", param="..param)
	if flag == 1 then
		ShowVipData( sysarg )
	elseif flag == 2 then
		GetVipGradeGift( sysarg, param )
	elseif flag == 3 then
		SendVipGiftData( sysarg )
	elseif flag == 4 then
		local privValues = GetVipGradePrivValue( sysarg, param )
		for i, privValue in ipairs( privValues ) do
			print("GmDoVip, privValue="..privValue)
		end
	end
end

function ShowVipData( sysarg )
	local vipGrade 		= Actor.getVipGrade( sysarg )
	local drawYbCount 	= Actor.getUIntProperty(sysarg, PROP_ACTOR_DRAW_YB_COUNT)

	print("ShowVipData, vipGrade="..vipGrade..", drawYbCount="..drawYbCount)
end

--[[
注册一些事件
]]
function ResisterVipBossProc()
	--print("ResisterVipBossProc...")
	for vipGradeIdx, vipGradeCfg in ipairs( VipConfig.VipGrade ) do
		if vipGradeCfg.boss then
			--print("ResisterVipBossProc, sceneId="..vipGradeCfg.boss.sceneId..", fubenId="..vipGradeCfg.boss.fubenId)
			SetNeedReloginFuben( vipGradeCfg.boss.fubenId, GlobalLuaConfig.reloginFuben.vipBoss, GetElemsForReLoginVipBoss ) --该副本是需要掉线恢复的
			SceneExitDispatcher.register( vipGradeCfg.boss.sceneId, OnCommonSceneExit )					--注册离开场景、副本后触发的事件
			SceneHandExitDispatcher.register( vipGradeCfg.boss.sceneId, OnCommonSceneHandExit )			--注册手工离开场景、副本后触发的事件
			SceneLogoutDispatcher.register( vipGradeCfg.boss.sceneId, OnCommonFubenLogout )				--注册场景、副本中下线触发的事件
			return
		end
	end
end

function OnVipDailyProc( sysarg )
	local sVar = Actor.getStaticVar(sysarg)
	sVar.vipBoss = nil
end

--[[
掉线恢复的一些配置
]]
function GetElemsForReLoginVipBoss( fubenPtr )
	local vipLevel 	= Fuben.getFbValue( fubenPtr, VIP_FUBEN_VIPLEVEL )
	--print("GetElemsForReLoginVipBoss, vipLevel="..vipLevel)
	local vipGradeCfg = GetVipGradeCfgByVipLevel( vipLevel )
	if not vipGradeCfg or not vipGradeCfg.boss then
		return nil
	end

	local fubenCfg 			= vipGradeCfg.boss
	local FuncAfterEnter 	= InitVipBossPanel						--进入场景后要执行的方法
	local loginLog   		= "vip boss login" 
	local loginLogOk		= "vip boss login ok" 
	return fubenCfg, FuncAfterEnter, loginLog, loginLogOk
end

function GetVipGradeCfgByVipLevel( VipLevel )
	for vipGradeIdx, vipGradeCfg in ipairs( VipConfig.VipGrade ) do
		if vipGradeCfg.vipLevel == VipLevel then
			--print("GetVipGradeCfgByVipLevel, vipLevel="..vipGradeCfg.vipLevel)
			return vipGradeCfg
		end
	end
	return nil
end

--[[
此时PROP_ACTOR_DRAW_YB_COUNT已经是最新的充值元宝数量
]]
function OnVipActorChargeYuanBao( sysarg, yuanbao )
	local drawYbCount 	= Actor.getUIntProperty(sysarg, PROP_ACTOR_DRAW_YB_COUNT)		--注意，此属性已经变化了
	--local allDrawYbCount= drawYbCount + yuanbao
	--print("OnVipActorChargeYuanBao, drawYbCount="..drawYbCount..", yuanbao="..yuanbao..", allDrawYbCount="..allDrawYbCount)
	--print("OnVipActorChargeYuanBao, drawYbCount="..drawYbCount)
	local vipCfg 	= GetVipCfgByDrawYuanBao( drawYbCount )
	local vipGrade 	= Actor.getVipGrade( sysarg )
	--print("OnVipActorChargeYuanBao, vipGrade="..vipGrade..", vipLevel="..vipCfg.vipLevel)
	if vipCfg and vipCfg.vipLevel > vipGrade then
		Actor.setVipGrade( sysarg, vipCfg.vipLevel )
	end
end

function GetVipCfgByDrawYuanBao( drawYbCount )
	local maxVipGrade = #VipConfig.VipGrade
	for vipGrade=1, maxVipGrade do
		local level = maxVipGrade - vipGrade + 1
		local vip = VipConfig.VipGrade[level]
		if vip and vip.needYuanBao > 0 and drawYbCount >= vip.needYuanBao and not vip.forExperience then		--必须是正式VIP
			return vip
		end
	end
	return nil
end

--[[VIP 等级变化了（串行VIP）
]]
function OnVipGradeChanged( sysarg, oldVipGrade, newVipGrade )
	--print("OnVipGradeChanged, oldVipGrade="..oldVipGrade..", newVipGrade="..newVipGrade)
	--清除原特权buff，再增加对应特权buff
	for i, buffId in ipairs( VipConfig.upgradeVipBuff ) do
		if Actor.hasBuffById( sysarg, buffId ) then
			Actor.delBuffById( sysarg, buffId )
		end
	end

	AddVipPrivileges( sysarg )

	TitleEventTrigger(sysarg, HeadTitleEvent.Vip, newVipGrade)

	OfflineExpActorVipLevelChange(sysarg) --离线副本vip改变

	vipOperationChangeHandler(sysarg, newVipGrade)

	vipSceneOnVipChange(sysarg)

	local progress = Actor.getIntProperty(sysarg , PROP_ACTOR_DRAW_YB_COUNT)
	OpenServerPKRankTriggleEvent(sysarg, enOpenServerRank_VIP, newVipGrade, progress)

	NationwideRedPacketsVipChange(sysarg, oldVipGrade, newVipGrade)
end

--[[
成为体验VIP
]]
function OnVipExperience( sysarg )
	for i, buffId in ipairs( VipConfig.upgradeVipBuff ) do
		if Actor.hasBuffById( sysarg, buffId ) then
			Actor.delBuffById( sysarg, buffId )
		end
	end

	AddVipPrivileges( sysarg )
end

--[[
增加VIP特权
]]
function AddVipPrivileges( sysarg )
	local vipGrade 	= Actor.getVipGrade(sysarg)
	--print("AddVipPrivileges, vipGrade="..vipGrade)
	local vipCfg = nil
	if vipGrade > 1 then										--正式VIP
		vipCfg = VipConfig.VipGrade[vipGrade+1]
	elseif Actor.isInVipExperienceTime( sysarg ) then			--体验VIP
		vipCfg = VipConfig.VipGrade[1]  						--第1个是体验VIP
	elseif vipGrade == 1 then									--VIP1级低于体验VIP
		vipCfg = VipConfig.VipGrade[2]
	end

	if not vipCfg then
		return
	end

	for i, vipPriv in ipairs( vipCfg.privilege ) do
		AddOneVipPrivilege( sysarg, vipPriv )
	end
	SendActorAllDailyOpTimes( sysarg )		--VIP变化，通知客户端
end

function AddOneVipPrivilege( sysarg, vipPriv )
	--print("AddOneVipPrivilege..")
	if vipPriv.privId == enVipPriv_Buff then						--加Buff
		local buffs = GetVipGradePrivValue( sysarg, enVipPriv_Buff ) or {}			--可能有多个buff
		local job = Actor.getIntProperty(sysarg, PROP_ACTOR_VOCATION)
		for i,buffId in ipairs( buffs[job] or {} ) do
			if buffId > 0 then
				--print("AddOneVipPrivilege, buffId="..buffId)
				Actor.addBuffById( sysarg, buffId )
			end
		end
	end
end

--[[
领取VIP礼包
gradeGiftIdx：第N个礼包
]]
function GetVipGradeGift( sysarg, gradeGiftIdx )
	if gradeGiftIdx <= 1 then 		--第1个是体验VIP，没有礼包
		return
	end
	local vipCfg = VipConfig.VipGrade[gradeGiftIdx]
	if not vipCfg then
		return
	end

	local sVar = Actor.getStaticVar(sysarg)
	if not sVar.vipGradeGift then
		sVar.vipGradeGift = {}
	end

	if not Actor.isVip( sysarg ) then
		Actor.sendTipmsg(sysarg, Lang.ScriptTips.COM0017,ttFlyTip)
		return
	end

	local giftIdx = gradeGiftIdx - 1 		--去掉第1个体验的礼包
	local vipGrade = Actor.getVipGrade(sysarg)
	if giftIdx > vipGrade then
		Actor.sendTipmsg(sysarg, Lang.ScriptTips.COM0018,ttFlyTip)
		return
	else
		local getNum = sVar.vipGradeGift[giftIdx] or 0		--sVar.vipGradeGift是从1-12的正式VIP
		if getNum > 0 then
			Actor.sendTipmsg(sysarg, Lang.ScriptTips.Vip001,ttFlyTip)
			return
		end
	end

	local consume = {{type = 10 , id = 0,  count = vipCfg.vipGiftYb}}

	if not CheckConsumeCond( sysarg, consume, 1, false ) then
		return
	end

	local needGrid = CheckBagGridForAwardsEx(sysarg, vipCfg.vipGift)
	if needGrid > 0 then
		Actor.sendTipmsg(sysarg, Lang.ScriptTips.COM0015, ttFlyTip)
		return
	end

	if not DoConsumeCond( sysarg, consume, 1, GameLog.clVipGetGradeGift, Lang.ScriptTips.VipLog001, false ) then
		return
	end

	GiveCommonAwardEx(sysarg, vipCfg.vipGift, GameLog.clVipGetGradeGift, Lang.ScriptTips.VipLog001)

	sVar.vipGradeGift[giftIdx] = 1
	SendVipGiftData( sysarg )

	--System.broadcastTipmsg( string.format(Lang.ScriptTips.Vip002, Actor.getName(sysarg), giftIdx ), ttScreenCenter + ttChatWindow )
end

--[[
下发VIP礼包数据
]]
function SendVipGiftData( sysarg )
	local sVar = Actor.getStaticVar(sysarg)
	if not sVar.vipGradeGift then
		sVar.vipGradeGift = {}
	end

	local netPack = DataPack.allocPacket( sysarg, enVipSystem, sSendVipInfo )
	if netPack then
		local count = #VipConfig.VipGrade-1				--第1个是体验VIP，不算
		DataPack.writeChar(netPack, count)
		for giftIdx=1, count do
			local vipCfg = VipConfig.VipGrade[giftIdx+1] 			--VIP配置从2开始
			if vipCfg.vipLevel > 0 then			--正式VIP
				local getNum = sVar.vipGradeGift[giftIdx] or 0		--礼包从1开始
				DataPack.writeChar(netPack, giftIdx)
				DataPack.writeChar(netPack, getNum)
				--print("SendVipGiftData, giftIdx="..giftIdx..", getNum="..getNum)
			end
		end
	end
	DataPack.flush(netPack)
end


--[[
下发VIP礼包数据
]]
function SendVipBossData( sysarg )
	--print("SendVipBossData, ...")
	local netPack = DataPack.allocPacket( sysarg, enVipSystem, sSendVipBossData )
	if netPack then
		local count = 0
		for vipGrade, vipCfg in ipairs( VipConfig.VipGrade ) do
			if vipCfg.boss then
				count = count + 1
			end
		end

		DataPack.writeChar(netPack, count)
		for vipGradeIdx, vipCfg in ipairs( VipConfig.VipGrade ) do 		--vipGradeIdx是从1-13，第一个是体验VIP
			if vipCfg.boss then
				local doneNum = GetActorDailyDoneTimes( sysarg, enDailyOpTimes_VipBoss, vipCfg.vipLevel )
				DataPack.writeChar(netPack, vipCfg.vipLevel)
				DataPack.writeChar(netPack, doneNum)
				--print("SendVipBossData, vipGrade="..vipCfg.vipLevel..", doneNum="..doneNum)
			end
		end
	end
	DataPack.flush(netPack)
end

--[[
进入VIPBOSS
]]
function EnterVipBoss( sysarg, vipLevel )
	--print("EnterVipBoss, vipLevel="..vipLevel)
	local vipGradeCfg = GetVipGradeCfgByVipLevel( vipLevel )
	if vipGradeCfg and vipGradeCfg.boss then
		local bossCfg = vipGradeCfg.boss
		if CommonEnterScence( sysarg, bossCfg, 0, "", VipBossCheckEnter, VipBossBeforeEnter ) then
			local fubenPtr = Actor.getFubenPrt( sysarg )
			Fuben.setFbValue( fubenPtr, VIP_FUBEN_VIPLEVEL, vipLevel )

			Actor.closeDialogs( sysarg, enLinkVipBossWnd )
			InitVipBossPanel( sysarg, bossCfg )

			SendVipBossData( sysarg )
		end
	end
end

function InitVipBossPanel( sysarg, bossCfg )
	local contStyle 	= 2					--只显示剩余时间
	local buttons 		= {1}
	local activityId 	= enRightPanelActivity_VipBoss
	local title			= Lang.ScriptTips.VipBossName
	OpenCommonStylePanel( sysarg, 1, activityId, bossCfg.vipLevel, title, bossCfg, contStyle, buttons  )
end

function ChangeVipBossPanel( sysarg, bossCfg )
	local activitySts 	= 2
	local contStyle 	= 2
	local buttons1 		= {1}
	local buttons2 		= {1}
	ChangeCommonStylePanel( sysarg, activitySts, contStyle, bossCfg, buttons1, buttons2 )
end

function VipBossCheckEnter( sysarg, sceneCfg )
	--要检查进入次数
	if not CheckActorDailyDoneTimes( sysarg, enDailyOpTimes_VipBoss, sceneCfg.vipLevel ) then
		Actor.sendTipmsg( sysarg, Lang.ScriptTips.COM0021, ttFlyTip)
		return false
	end
	return true
end

function VipBossBeforeEnter( sysarg, sceneCfg )
	--正式副本要增加进入次数
	--AddActorDailyDoneTimes( sysarg, enDailyOpTimes_VipBoss, sceneCfg.vipLevel, 1 )		--进入次数+1
	return true
end

--[[
--sysarg:归属玩家
--lastHitKiller：最后一击玩家
注意：怪物死亡时，玩家可能不在线，要确保副本进度能够继续进行
]]
function OnVipBossDie( monster, sysarg, monId, lastHitKiller )
	local fubenPtr  = Actor.getFubenPrt( monster )
	local vipLevel 	= Fuben.getFbValue( fubenPtr, VIP_FUBEN_VIPLEVEL )
	--print("OnVipBossDie, vipLevel="..vipLevel)
	local vipGradeCfg = GetVipGradeCfgByVipLevel( vipLevel )
	if not vipGradeCfg or not vipGradeCfg.boss then
		return nil
	end
	AddActorDailyDoneTimes( sysarg, enDailyOpTimes_VipBoss, vipGradeCfg.boss .vipLevel, 1 )
	SendVipBossData(sysarg)
	ChangeVipBossPanel( sysarg, vipGradeCfg.boss )
end

--[[
一项特权对应的值可能有多个
]]
function GetVipGradePrivValue( sysarg, privId, param )
	--print("GetVipGradePrivValue, privId="..privId..", param="..param)
	if not privId then
		return 0			--无特权
	end
	local vipGrade = Actor.getVipGrade( sysarg )
	if vipGrade <= 0 and not Actor.isInVipExperienceTime(sysarg) then	--不是VIP，并且不在体验VIP期间内
		return 0			--非VIP，无特权
	end

	local vipGradeCfg = nil
	if vipGrade > 1 then										--正式VIP
		vipGradeCfg = VipConfig.VipGrade[vipGrade+1]
	elseif Actor.isInVipExperienceTime( sysarg ) then			--体验VIP
		vipGradeCfg = VipConfig.VipGrade[1]  					--第1个是体验VIP
	elseif vipGrade == 1 then									--VIP1级低于体验VIP
		vipGradeCfg = VipConfig.VipGrade[2]
	end

	if not vipGradeCfg then
		return 0			--无对应等级，无特权
	end

	local privCfg = vipGradeCfg.privilege[privId]
	if not privCfg then
		return 0			--无特权
	end

	if privId == enVipPriv_UseItem then
		local itemId = param
		return vipGradeCfg.privUseItem[itemId] or 0
	end
	return privCfg.privValue
end

--[[体验VIP到期
单纯通知客户端，
不删除已经增加的buf
]]
function OnExepVipExpire( sysarg )
	--print("OnExepVipExpire ......")
	local pack = DataPack.allocPacket(sysarg, enDefaultEntitySystemID, sExepVipExpired)
	if pack then
		DataPack.flush(pack)
	end
end

ResisterVipBossProc()
